package cn.iocoder.yudao.module.erp.controller.app.erpSupplierguide;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.erpSupplierguide.vo.ErpSupplierGuidePageReqVO;
import cn.iocoder.yudao.module.erp.controller.app.erpSupplierguide.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.erpSupplierguide.ErpSupplierGuideDO;
import cn.iocoder.yudao.module.erp.service.erpSupplierguide.ErpSupplierGuideService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "ERP供应商指导价接口")
@RestController
@RequestMapping("/erp/supplier/guide")
@Validated
@Slf4j
public class AppErpSupplierGuideController {

    @Resource
    private ErpSupplierGuideService supplierGuideService;

    @GetMapping("/page")
    @Operation(summary = "获得ERP 供应商指导价分页")
    public CommonResult<PageResult<ErpSupplierGuideRespVO>> getSupplierGuidePage(@Valid PageParam pageReqVO) {
        PageResult<ErpSupplierGuideDO> pageResult = supplierGuideService.getAppSupplierGuidePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpSupplierGuideRespVO.class));
    }



    @GetMapping("/exportExcelTemplate")
    @Operation(summary = "导出ERP 供应商指导价模板")
    @PermitAll
    public void exportExcelTemplate(HttpServletResponse response) throws IOException {
        // 设置响应头，确保浏览器直接下载文件
        // 导出 Excel
        ExcelUtils.appWrite(response, "供应商指导价模板.xls", "数据", ErpSupplierGuideExportTemplateVO.class,
                BeanUtils.toBean(new ArrayList<>(), ErpSupplierGuideExportTemplateVO.class));
    }


    @PostMapping("/import")
    @Operation(summary = "批量导入指导价")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
    })
    public CommonResult<SupplierGuideImportRespVO> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        supplierGuideService.checkSupplierStatus();
        List<SupplierGuideImportExcelVO> list = ExcelUtils.read(file, SupplierGuideImportExcelVO.class);
        log.info("导入数据：{}", list);
        return success(supplierGuideService.importList(list));
    }


    @PostMapping("/update")
    @Operation(summary = "根据id更新供应商指导价")
    public CommonResult<Boolean> updateSupplierGuide(@Valid @RequestBody ErpSupplierGuideUpdateReqVO updateReqVO) {
        supplierGuideService.updateAppSupplierGuide(updateReqVO);
        return success(true);
    }


    @GetMapping("/exportExcel")
    @Operation(summary = "导出ERP 供应商指导价 Excel")
    public void exportSupplierGuideExcel(@Valid ErpSupplierGuidePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
//        supplierGuideService.checkSupplierStatus();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpSupplierGuideDO> list = supplierGuideService.exportSupplierGuideExcel();
        // 导出 Excel
        ExcelUtils.appWrite(response, "供应商指导价.xls", "数据", ErpSupplierGuideExportVO.class,
                        BeanUtils.toBean(list, ErpSupplierGuideExportVO.class));
    }

}
